package com.pfzy.ui.activity.notification

import android.content.Context
import android.os.Build
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.pfzy.ui.R

class NotificationDialog4 {
    private var mContext: Context? = null
    private var mDialog: AlertDialog? = null
    private var mListener: OnNotificationClick? = null

    constructor(context: Context) {
        _logd("constructor")
        mContext = context
        var builder = AlertDialog.Builder(context, R.style.dialog_notifacation_top)
        mDialog = builder.create()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mDialog!!.getWindow()!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
        } else {
            mDialog!!.getWindow()!!.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
        }
        mDialog!!.getWindow()!!.setGravity(Gravity.TOP)

        val layoutParams = mDialog!!.getWindow()!!.attributes
        layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
        layoutParams?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        layoutParams?.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        mDialog!!.getWindow()!!.attributes = layoutParams
        mDialog!!.getWindow()!!.setWindowAnimations(R.style.dialog_animation)

        mDialog!!.setCanceledOnTouchOutside(true)
    }

    fun showNotificationDialog(
        title: String,
        content: String,
        image: Int,
        listener: OnNotificationClick? = null
    ) {
        _logd("showNotificationDialog")
        mDialog!!.show()
        var mView = View.inflate(mContext, R.layout.layout_notifacation3, null)
        var mTvTitle = mView!!.findViewById<TextView>(R.id.layout_notifacation3_title)
        var mTvContent = mView!!.findViewById<TextView>(R.id.layout_notifacation3_content)
        var mImage = mView!!.findViewById<ImageView>(R.id.layout_notifacation3_image)
        var mTvTime = mView!!.findViewById<TextView>(R.id.layout_notifacation3_time)

        mDialog!!.getWindow()!!.setContentView(mView)
        if (title.isNotEmpty()) {
            mTvTitle.text = title
        }

        if (content.isNotEmpty()) {
            mTvContent.text = content
        }

        if (image != 0) {
            mImage.setImageResource(image)
        }
    }

    fun setOnNotificationClickListener(listener: OnNotificationClick) {
        mListener = listener
    }

    interface OnNotificationClick {
        fun onClick()
    }

    fun _logd(msg: String) {
        Log.d("全局弹窗", msg)
    }

    companion object {
        @Volatile
        private var instance: NotificationDialog4? = null

        @JvmStatic
        fun getInstance(context: Context): NotificationDialog4? {
            if (instance == null) {
                synchronized(NotificationDialog4::class) {
                    if (instance == null) {
                        instance = NotificationDialog4(context)
                    }
                }
            }
            return instance
        }

        @JvmStatic
        public fun show(context: Context, title: String, content: String, image: Int) {
            var builder = AlertDialog.Builder(context, R.style.dialog_notifacation_top)
            var dialog = builder.create()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                dialog.getWindow()!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
            } else {
                dialog.getWindow()!!.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
            }
            dialog.getWindow()!!.setGravity(Gravity.TOP)
            val layoutParams = dialog.getWindow()!!.attributes
            layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
            layoutParams?.height = ViewGroup.LayoutParams.WRAP_CONTENT
            layoutParams?.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            dialog.getWindow()!!.attributes = layoutParams
            dialog.getWindow()!!.setWindowAnimations(R.style.dialog_animation)

            dialog.setCanceledOnTouchOutside(true)

            dialog.show()
            var mView = View.inflate(context, R.layout.layout_notifacation3, null)
            var mTvTitle = mView!!.findViewById<TextView>(R.id.layout_notifacation3_title)
            var mTvContent = mView!!.findViewById<TextView>(R.id.layout_notifacation3_content)
            var mImage = mView!!.findViewById<ImageView>(R.id.layout_notifacation3_image)
            var mTvTime = mView!!.findViewById<TextView>(R.id.layout_notifacation3_time)

            dialog.getWindow()!!.setContentView(mView)
            if (title.isNotEmpty()) {
                mTvTitle.text = title
            }

            if (content.isNotEmpty()) {
                mTvContent.text = content
            }

            if (image != 0) {
                mImage.setImageResource(image)
            }
        }
    }
}